package com.swufe.week6;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MylistActivity4 extends ListActivity implements Runnable{
    private  static  final  String TAG="MylistActivity4";
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_mylist3);
        List<String> list1=new ArrayList<>();
        for (int i = 1;i<100;i++){
            list1.add("列表"+ i);
        }
        // String[] list_one={"锁屏","显示","声音与振动","通知管理","桌面"};
        ListAdapter adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,list1);
        setListAdapter(adapter);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==9){
                    ArrayList<String> list=(ArrayList<String>)msg.obj;
                    ListAdapter adapter=new ArrayAdapter<String>(MylistActivity4.this,
                            android.R.layout.simple_list_item_1,list);
                    setListAdapter(adapter);
                    Toast.makeText(MylistActivity4.this,"ret size="+list.size(),
                            Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };
        Thread thread=new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        Log.i(TAG, "run: .........");
        //URL url=null;
        List<String> ret =new ArrayList<>();
        try {
            String url = "https://www.boc.cn/sourcedb/whpj/";
            Document doc = Jsoup.connect(url).get();
            //Log.i(TAG, "run: " + doc.title());
            Element tables = doc.getElementsByTag("table").get(1);
            Elements trs=tables.getElementsByTag("tr");
            for(Element tr:trs){
                Elements tds=tr.getElementsByTag("td");
                if(tds.size()>0){
                    String str =tds.first().text();
                    String val=tds.get(5).text();
                    Log.i(TAG, "run: td="+tds.first().text());
                    Log.i(TAG, "run: rate="+tds.get(5).text());
                    ret.add(str+"--->"+val);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回给主线程
        Message msg = handler.obtainMessage(9,ret);
        handler.sendMessage(msg);

    }
}